<?
class itemModel extends db implements db_interface{
private static $instance;

public function __construct($iDb){
	
		parent::__construct($iDb);
	
	}
	
public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	
	
	}
	
public function fill(){


		$query = $this -> db -> query('SELECT b.bew_id,b.bew_titel,i.item_id,i.item_title,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date,i.item_type,a.album_type FROM bewonersgroep b join item i on(b.bew_id = i.item_geb_id) left join album a on(i.item_id = a.album_id)  where i.item_deleted = 1 order by i.item_id desc limit 50;');
		
		
		$returnArray = array();
		
		
		while ($row = $query->fetch_assoc())
		{
			$item = new item();
			switch ($row['item_type'])
			{
				case 'bericht':
					$myModel = new item_bericht($this -> db);
					$myModel -> id = $row['item_id'];
					$myModel -> fill();
					$item -> type = 'tekstberichten';
					break;
				case 'foto':
					$myModel = new item_foto($this -> db);
					$myModel -> id = $row['item_id'];
					$myModel -> fill();
					$item -> type = 'fotos';
					break;
				case 'video':
					$myModel = new item_video($this -> db);
					$myModel -> id = $row['item_id'];
					$myModel -> fill();
					$item -> type = 'videos';
					break;
				case 'activiteit':
					$myModel = new item_activiteiten($this -> db);
					$myModel -> id = $row['item_id'];
					$myModel -> fill();
					$item -> type = 'activiteiten';
					break;
				case 'zoeker':
					$myModel = new item_zoeker($this -> db);
					$myModel -> id = $row['item_id'];
					$myModel -> fill();
					$item -> type = 'zoekertjes';
					break;
				case 'album':
					$myModel = new item_album($this -> db);
					$myModel -> id = $row['item_id'];
					$myModel -> fill();
					$item -> type = $row['item_type'];
					break;
			}
			$item -> item = $myModel;
						$returnArray[]= $item;
		}
		return $returnArray;	
	
	}
	public function fillById($bewId)
	{
		$query = $this -> db -> prepare('SELECT item_type,item_id FROM item where item_deleted = 1 and item_geb_id = ? ORDER BY item_id DESC LIMIT 10;');
		$bindValue = $bewId;
		
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($item_type,$id); 
		
		$returnArray = array();
		$items = array();
		while($query -> fetch())
		{	
		
			$items[] = array('type' => $item_type, 'id' => $id);
			
		}
	
		foreach($items as $value)
		{
			
			$bool = false;
			switch ($value['type'])
			{
				case 'bericht':
					$myModel = new item_bericht($this -> db);
					$myModel -> id = $value['id'];
					$myModel -> fill();
					break;
				case 'foto':
					$myModel = new item_foto($this -> db);
					$myModel -> id = $value['id'];
					$myModel -> fill();
					
					break;
				case 'video':
					$myModel = new item_video($this -> db);
					$myModel -> id = $value['id'];
					$myModel -> fill();
					if($myModel -> youtube_id ==''){
						$bool = true;
					}
					break;
				case 'activiteit':
					$myModel = new item_activiteiten($this -> db);
					$myModel -> id = $value['id'];
					$myModel -> fill();
					break;
				case 'zoeker':
					$myModel = new item_zoeker($this -> db);
					$myModel -> id = $value['id'];
					$myModel -> fill();
				break;
				case 'album':
					$bool = true;
				break; 
			}
			
		
		
			if(!$bool){
					
						$returnArray[]= $myModel;
						
			}
		}
		
		return $returnArray;	
	
	}
	
	
	public function verwijder($item){
	
		$query = $this -> db -> prepare("CALL itemDel(?)");
		$bind = $item -> id;
		$query -> bind_param('i', $bind);
		$query -> execute();
		
		
		
	
	
	}
	
	
}


?>